package com.xy.manage.dao.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.xy.common.page.Pagination;
import com.xy.core.dao.impl.BaseDaoImpl;
import com.xy.manage.dao.TmembernewsDao;
import com.xy.manage.model.Tmembernews;

public class TmembernewsDaoImpl extends BaseDaoImpl<Tmembernews> implements
		TmembernewsDao {
	public TmembernewsDaoImpl() {
		super(Tmembernews.class);
	}

	@Override
	public List<Tmembernews> getTmembernews(Map<String, Object> map) {
		Object[] values = map.values().toArray();
		List<Tmembernews> localList = findByHql(getHql(map, null, null), values);
		return localList;
	}

	@Override
	public Pagination getTmembernewsByPage(Map<String, Object> map, int pageNo,
			int pageSize, String orderby, String order) {
		Object[] values = map.values().toArray();
		return findByPage(getHql(map, orderby, order), values, pageNo, pageSize);
	}

	private String getHql(Map<String, Object> map, String orderby, String order) {
		StringBuffer str = new StringBuffer("from Tmembernews tt");
		if (map.size() > 0)
			str.append(" where ");
		Set<String> set = map.keySet();
		StringBuffer where = new StringBuffer();
		for (String name : set) {
			if (where.length() > 0)
				where.append(" and  ");
			where.append("tt.").append(name).append("=?");
		}
		if (orderby != null && !"".equals(orderby)) {
			str.append(where.toString()).append("  order by ").append(orderby);
			if (order != null && !"".equals(order)) {
				str.append(" ").append(order);
			} else {
				str.append(" asc ");
			}
		}
		return str.toString();
	}
}